/*
* $Id: UserFieldsTable.cpp 152 2010-09-23 18:28:55Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "UserFieldsTable.h"
#include "TableNames.h"
#include "../SqlBinder.h"

CUserFieldsTable::CUserFieldsTable()
: CTable(USER_FIELDS_TABLE_NAME){}
CUserFieldsTable::~CUserFieldsTable(){}

void CUserFieldsTable::CreateTable(IDatabase* pDatabase){
	std::tstring strRequest(_T("CREATE TABLE IF NOT EXISTS "));
	pDatabase->ExecRequest(strRequest + GetNameTable() + 
							_T("(itable INTEGER NOT NULL,")\
							_T("name TEXT NOT NULL,")\
							_T("type INTEGER NOT NULL);"));
}
void CUserFieldsTable::UpdateValue(IDatabase* pDatabase){
		std::tstring strRequest(_T("UPDATE "));
	CSqlBinder binder(strRequest + GetNameTable() + 
					_T(" SET type=? WHERE itable=? AND name=?"));
	binder.SetValue(2, m_content.s_strName);
	binder.SetValue(0, m_content.s_type);
	binder.SetValue(1, m_content.s_table);
	pDatabase->ExecRequest(binder.GetRequest());
	m_content.Clear();
}
IResult* CUserFieldsTable::GetValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("SELECT * FROM "));
	if(m_content.s_table){
		strRequest = strRequest + GetNameTable() + _T(" WHERE itable=?");
		binder.SetValue(0, m_content.s_table);
		if(!m_content.s_strName.empty()){
			strRequest = strRequest + _T(" AND name=?");
			binder.SetValue(1, m_content.s_strName);
		}
		binder.SetRequest(strRequest);
	}else
		binder.SetRequest(strRequest + GetNameTable());
	m_content.Clear();
	return FillContent(pDatabase->ExecRequest(binder.GetRequest()));
}
void CUserFieldsTable::SetValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("INSERT INTO "));
	binder.SetRequest(strRequest + GetNameTable() + _T(" VALUES(?, ?, ?)"));
	binder.SetValue(0, m_content.s_table);
	binder.SetValue(1, m_content.s_strName);
	binder.SetValue(2, m_content.s_type);
	m_content.Clear();
	pDatabase->ExecRequest(binder.GetRequest());
}
void CUserFieldsTable::DeleteValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("DELETE FROM "));
	if(m_content.s_table){
		strRequest = strRequest + GetNameTable() + _T(" WHERE itable=?");
		binder.SetValue(0, m_content.s_table);
		if(!m_content.s_strName.empty()){
			strRequest = strRequest + _T(" AND name=?");
			binder.SetValue(1, m_content.s_strName);
		}
		binder.SetRequest(strRequest);
	}else{
		binder.SetRequest(strRequest + GetNameTable());
	}
	m_content.Clear();
	pDatabase->ExecRequest(binder.GetRequest());
}
IOperation::i_content* CUserFieldsTable::CreateContent(){
	return new content;
}

void CUserFieldsTable::content::Clear(){
	s_strName.clear();
	s_table = TypeField::DEFAULT;
	s_type = TypeField::STRING;
}
void CUserFieldsTable::content::Set(const std::vector<CTableItem>& aContent){
	for(std::vector<CTableItem>::const_iterator it = aContent.begin(); it != aContent.end(); it++){
		if(it->m_strNameColumn == _T("itable"))
			s_table = (TypeField::table)_ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("name"))
			s_strName = it->m_strValueColumn;
		else if(it->m_strNameColumn == _T("type"))
			s_type = (TypeField::type)_ttoi(it->m_strValueColumn.c_str());
	}
}
void CUserFieldsTable::content::operator=(const content& cnt){
	s_strName = cnt.s_strName;
	s_table = cnt.s_table;
	s_type = cnt.s_type;
}
bool CUserFieldsTable::content::operator==(const content& cnt){
	return s_strName == cnt.s_strName;
}
void CUserFieldsTable::content::Free(){
	delete this;
}